package com.custom.tags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.TagSupport;

import com.DAO.NoticeDAO;
import com.beans.NoticeForm;
 
public class NoticeTagHandler1 extends TagSupport {
    
	String cursor; 
	String category;
 
	public String getCategory() {
		return category;
	}


	public void setCategory(String category) {
		this.category = category;
	}


	public String getCursor() {
		return cursor;
	}


	public void setCursor(String cursor) {
		this.cursor = cursor;
	}


	@Override
    public int doStartTag() throws JspException {
         
        try {
            //Get the writer object for output.
            JspWriter out = pageContext.getOut();
            try{
                NoticeDAO noticeDAO=new NoticeDAO();
                System.out.println("category is.."+category+"  cursor is..."+cursor);
                
                
                ArrayList<NoticeForm> noticeList=noticeDAO.getPageData(Integer.parseInt(cursor),Integer.parseInt(category));               
                
                
                Iterator<NoticeForm> itr=noticeList.iterator();
                                 
                out.println("<div id = \"noticelist\">\n<ul>");
                 
                 
                 if(!itr.hasNext()){
                	 out.print("<div id = \"noticeheading\">");
                	 
                	 out.println("<center><b>No Post Yet</b></center>");
                	 
                	 out.print("</div>");
                	 
                	 
                 }
                 
                 while(itr.hasNext()) {
                	out.println("<li>");
                	NoticeForm noticeForm=itr.next();
					int id=noticeForm.getNoticeId();
					String title=noticeForm.getTitle();
					String description=noticeForm.getDescription();
					String creator=noticeForm.getCreatorName();
					
					String expdate=noticeForm.getCreatedDate().toString();
					String d[]=expdate.split("-");
					expdate=d[2]+"/"+d[1]+"/"+d[0];
					
					int Category_id=noticeForm.getCategoryId();				
					
					out.print("<div id = \"noticeheading\">");
					out.println("<a href='TitleDescription.do?noticeid="+id+"'>" +
							"<img alt = \"notice\" src = \"image/33.png\" height = \"20\" width = \"20\" align = \"top\" > "+title+"</a>" +
							"<div id = \"maintxt\"> by <i>"+creator+"</i> on <i>"+ expdate +"</i> ");
					out.print("</div>\n<div id = \"noticedescription\">");
					
					if(description.length()<=80){					
					out.println(description+"<br>");
					}
					else
					{
						out.println(description.substring(0,80)+"...<br>");
					}
					
					out.print("</div>");
									
					out.println("</li>");
				}
                 out.println("</ul></div>");
                 
               }
                
                catch(Exception e){
                	e.printStackTrace();
                       	
                }
                           
            
            
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }


	
	
  
}